package com.witmore.skyline.web.exception;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MqttMessageHandler {

    @Autowired
    private AsyncWeChatNotifier asyncWeChatNotifier;
    @ServiceActivator(inputChannel = "wechatInboundChannel")
    public void handleMessage(Message<?> message) {
        log.info("Received WeChat MQTT message: {}", message.getPayload());
        JSONObject map =  JSONObject.parseObject(String.valueOf(message.getPayload()));
        // 发送企微群消息
        try {
            asyncWeChatNotifier.sendAIToWeChatAsync(map.getString("message"), map.getString("picUrl"));
        } catch (Exception e) {
            log.error("发送企微群消息失败，error msg-->{}", e.getMessage());
        }
    }
}